﻿using JNPF.Basics.Logic.Common.Login;
using JNPF.Basics.Models;
using JNPF.Utils;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic.Common.AppLogin
{
    public class AppLoginBll
    {
        private OrganizeBll organizeBll = new OrganizeBll();
        private PositionBll positionBll = new PositionBll();
        private LoginBll loginBll = new LoginBll();
        private FlowEngineBll FlowEngineBll = new FlowEngineBll();
        private RoleBll roleBll = new RoleBll();
        private UserBll userBll = new UserBll();

        private UserextendBll userextendBll = new UserextendBll();
        /// <summary>
        /// 【移动端】获取用户登录信息
        /// </summary>
        /// <param name="isCache"></param>
        /// <returns></returns>
        public async Task<AppLoginCurrentUserOutput> Get()
        {
            var output = new AppLoginCurrentUserOutput();
            var userInfo = UserProvider.Instance.Get();
            var userEntity = await userBll.GetInfo(userInfo.UserId);

            var zzhUser = await new jiushiZzhuserBll().GetInfoByUserId(userEntity.Id);

            UserextendEntity userextendEntity = await userextendBll.GetInfoByUserId(userInfo.UserId);

            var LoginCurrentUserModel = await loginBll.GetAuthorize();
            var flowEngineList = (await FlowEngineBll.GetList()).FindAll(x => x.EnabledMark == 1);
            output.menuList = LoginCurrentUserModel.moduleList.FindAll(x => x.Category == "App" && x.ParentId != "-1").MapToList<AppMenuModel>();
            output.flowFormList = flowEngineList.MapToList<AppFlowFormModel>();
            output.userInfo = GetUserInfo(userEntity, userextendEntity, zzhUser);
            return output;
        }


        /// <summary>
        /// 【移动端】获取用户登录信息
        /// </summary>
        /// <param name="isCache"></param>
        /// <returns></returns>
        public async Task<List<ModuleEntityNew>> GetMenuModels()
        {
            List<ModuleEntityNew> models = await loginBll.GetModule();

            List<ModuleEntityNew> moduleEntityNews = new List<ModuleEntityNew>();
            moduleEntityNews = models.FindAll(x => x.ParentId == "7110175e27ce424ea1d3ee4da7a72009");


            foreach (var item in moduleEntityNews)
            {
                item.ChildModule = models.FindAll(x => x.ParentId == item.Id);

            }
            return moduleEntityNews;
        }



        #region Method

        /// <summary>
        /// 登录信息(app)
        /// </summary>
        /// <returns></returns>
        private object GetUserInfo(UserEntity entity, UserextendEntity userextendEntity, jiushiZzhuserEntity jiushiZzhuserEntity)
        {
            var dictionary = new Dictionary<string, object>();
            if (entity.IsNotEmptyOrNull())
            {
                dictionary.Add("id", entity.Id);
                dictionary.Add("account", entity.Account);
                dictionary.Add("realName", entity.RealName);
                dictionary.Add("headIcon", "/api/Common/Image/userAvatar/" + entity.HeadIcon);
                dictionary.Add("gender", entity.Gender == 1 ? "男" : "女");
                dictionary.Add("roleName", roleBll.GetName(entity.RoleId));
                dictionary.Add("organizeName", organizeBll.GetCompany(entity.OrganizeId).Result);
                dictionary.Add("positionName", positionBll.GetName(entity.PositionId));
                dictionary.Add("birthday", entity.Birthday);
                dictionary.Add("mobilePhone", entity.MobilePhone);
                dictionary.Add("email", entity.Email);
                dictionary.Add("departmentName", organizeBll.GetName(entity.OrganizeId));
                dictionary.Add("shareImg", entity.ShareImg);
                dictionary.Add("nickName", entity.NickName);

                if (jiushiZzhuserEntity != null)
                {
                    dictionary.Add("IsFollow", jiushiZzhuserEntity.IsFollow);

                    dictionary.Add("OpenId", jiushiZzhuserEntity.OpenId);
                }
                else
                {
                    dictionary.Add("IsFollow", "");

                    dictionary.Add("OpenId", "");
                }

                if (userextendEntity != null)
                {
                    dictionary.Add("totalPointsEarned", userextendEntity.TotalPointsEarned);
                    dictionary.Add("pointsPendingWithdrawal", userextendEntity.PointsPendingWithdrawal);
                }
                else
                {
                    dictionary.Add("totalPointsEarned", 0);
                    dictionary.Add("pointsPendingWithdrawal", 0);

                }
            }
            return dictionary;
        }


        #endregion
    }
}
